package com.masterclient.app.modules.income.widget;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.income.ProfitHelpFragment;
import com.masterclient.app.modules.income.model.ProfitsTotalPopupBean;
import com.masterclient.app.modules.income.model.ProfitsTypePopupBean;
import com.masterclient.app.modules.timelimited.adapter.ViewPagerAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/6/16.
 */
@SuppressLint("ValidFragment")
public class ProfitHelpDialog extends DialogFragment {

    @BindView(R.id.dialog_profit_title)
    TextView dialogProfitTitle;
    @BindView(R.id.dialog_profit_viewpager)
    ViewPager dialogProfitViewpager;
    @BindView(R.id.dialog_profit_confirm)
    TextView dialogProfitConfirm;

    AppCompatActivity context;
    List<Fragment> fragments;
    ProfitsTotalPopupBean profitsTotalPopupBean;
    List<ProfitsTypePopupBean> profitsTypePopupBeans;


    public ProfitHelpDialog(@NonNull AppCompatActivity context,  ProfitsTotalPopupBean profitsTotalPopupBean,
                            List<ProfitsTypePopupBean> profitsTypePopupBeans) {
        super();
        this.context = context;
        this.profitsTotalPopupBean = profitsTotalPopupBean;
        this.profitsTypePopupBeans = profitsTypePopupBeans;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.dialog_profit_help, container, false);
        ButterKnife.bind(this,view);
        fragments = new ArrayList<>();
        if (profitsTypePopupBeans != null){
            dialogProfitTitle.setText(profitsTypePopupBeans.get(0).getTitle());
            for (ProfitsTypePopupBean bean : profitsTypePopupBeans) {
                List<ProfitsTypePopupBean> beans = new ArrayList<>();
                beans.add(bean);
                ProfitHelpFragment fragment = new ProfitHelpFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable("profits_type", (Serializable) beans);
                fragment.setArguments(bundle);
                fragments.add(fragment);
            }
        }else {
            dialogProfitTitle.setText(profitsTotalPopupBean.getTitle());

            ProfitHelpFragment fragment = new ProfitHelpFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("profits_total", (Serializable) profitsTotalPopupBean.getContent());
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }

        dialogProfitViewpager.setId(dialogProfitViewpager.hashCode());

        dialogProfitViewpager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(),fragments));

        return view;
    }


    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }


    @OnClick(R.id.dialog_profit_confirm)
    public void onViewClicked() {
        dismiss();
    }
}
